/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.utils;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import org.jdesktop.application.FrameView;

/**
 *
 * @author DOStudent1
 */
public class WindowHelpers {
    public static void allowEscapeRootPane(JDialog dialog, JRootPane rootPane) {
        KeyStroke keyStroke=KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        ActionListener escapeActionListener=new ActionListenerDialog(dialog);
        rootPane.registerKeyboardAction(escapeActionListener, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
   }

    public static void allowEscapeApplication(FrameView frameView) {
        KeyStroke keyStroke=KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        ActionListener escapeActionListener=new ActionListenerFrameView(frameView);
        frameView.getRootPane().registerKeyboardAction(escapeActionListener, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
   }

    private static class ActionListenerDialog implements ActionListener {

        JDialog dialog;
        public ActionListenerDialog(JDialog dialog) {
            this.dialog=dialog;
        }

        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
        }
    }
    
    
    private static class ActionListenerFrameView implements ActionListener {

        FrameView frameView;
        public ActionListenerFrameView(FrameView frameView) {
            this.frameView=frameView;
        }

        public void actionPerformed(ActionEvent e) {
            frameView.getApplication().exit();
        }
    }
}
